<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$veepariBookCr          = array();
$veepariBookDr          = array();
$tempArr                = array();
$tempArr2               = array();
$party                  = array();
$total                  = array();
$opening                = array();
$billDetailDebitArray   = array();
$billDetailCreditArray  = array();
$partyId                = 0;
$toDate                 = 0;
$fineToStore            = 0;
$amtToStore             = 0;
$crDrToStore            = '';
$opening['crFine']      = 0;
$opening['drFine']      = 0;
$opening['crAmount']    = 0;
$opening['drAmount']    = 0;
$toDate                 = 0;
$total['DrFine']        = 0;
$total['DrAmount']      = 0;
$total['CrFine']        = 0;
$total['CrLabour']      = 0;
$total['CrAmount']      = 0;
$msg                    = '';

if(isset($_POST['submit']))
{
	 $partyId        = isset($_POST['party']) ? $_POST['party'] : 0;
	 $partyName      = isset($_REQUEST['partyName']) ? $_REQUEST['partyName'] : 0;
	 $partyAddress   = ($_POST['partyAddress']) ? $_POST['partyAddress'] : 0;
	 $partyMobile    = ($_POST['partyMobile']) ? $_POST['partyMobile'] : 0;
	 $partyFromDate  = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;
   $updateQueryProperty = "UPDATE party
                              SET partyName  = '".$partyName."',
                                  partyAddress  = '".$partyAddress."',
                                  partyMobile  = '".$partyMobile."',
                                  partyFromDate  = '".$partyFromDate ."'
                            WHERE partyId  = '".$_POST['partyIdEdit']."'";
	  $updateQueryPropertyRequest = mysql_query($updateQueryProperty);
	  if(!$updateQueryPropertyRequest)
	    die("Update Query Not Inserted : ".mysql_error());
	  else
	 		header("Location:partyList.php");
}
	$partyId       = '';
	$partyName     = '';
	$partyAddress  = '';
	$partyMobile   = 0 ;
	$partyFromDate = '';
	
	$selectQueryParty = "SELECT partyId,partyName,partyAddress,partyMobile,partyFromDate
	                       FROM party
	                      WHERE partyId = ".$_REQUEST['partyId'];
	$selectQueryPartyResult = mysql_query($selectQueryParty);
	$selectQueryPartyNumRow = mysql_num_rows($selectQueryPartyResult);
	if($selectQueryPartyNumRow > 0)
	{
		while($partyRow = mysql_fetch_array($selectQueryPartyResult))
		{
			$partyId       = $partyRow['partyId'];
			$partyName     = $partyRow['partyName'];
			$partyAddress  = $partyRow['partyAddress'];
			$partyMobile   = $partyRow['partyMobile'];
			$partyFromDate = $partyRow['partyFromDate'];
		}
	}
/// Selection Of Vepaari Book Net Fine And Net Amount :Start
if(isset($_POST['party']) && $_POST['party'] > 0)
{
  $partyId   = $_POST['party'];
  $toDate    = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];

  $selectVeepariBook = "SELECT vepaaribilldetail.vepaariBookDetailId,vepaaribillmaster.vepaariBookMasterId,vepaaribillmaster.partyId,vepaaribillmaster.vepaariBookDate,vepaaribilldetail.fine,vepaaribilldetail.amount,vepaaribilldetail.CrDr
                          FROM vepaaribilldetail
                          JOIN vepaaribillmaster
                         WHERE vepaaribillmaster.partyId = ".$_POST['party']."
 	                         AND vepaaribillmaster.vepaariBookDate <= '".$toDate."'
	                         AND vepaaribilldetail.vepaariBookMasterId = vepaaribillmaster.vepaariBookMasterId
                      ORDER BY vepaariBookDate, vepaariBookDetailId";
  $selectVeepariBookRes = mysql_query($selectVeepariBook);
  $crCount = 0;
  $drCount = 0;
  $crCounter = 0;
  $drCounter = 0;
  
  while($vepaaribillDetailRow = mysql_fetch_assoc($selectVeepariBookRes))
  {
      if($vepaaribillDetailRow['CrDr'] == "Cr")
      {
      	$billDetailDebitArray[$crCounter]['fine']   = $vepaaribillDetailRow['fine'];
      	$billDetailDebitArray[$crCounter]['amount'] = $vepaaribillDetailRow['amount'];
        $drCounter++;
      }
      else
      {
      	$billDetailDebitArray[$drCounter]['fine']   = $vepaaribillDetailRow['fine'];
      	$billDetailDebitArray[$drCounter]['amount'] = $vepaaribillDetailRow['amount'];
      	$drCounter++;
      }
      if($vepaaribillDetailRow['CrDr'] == 'Cr')
	    {
	    	 $total['DrFine']      += $vepaaribillDetailRow['fine'];
	    	 $total['DrAmount']    += $vepaaribillDetailRow['amount'];
      }
      else
      {
      	 $total['CrFine']       += $vepaaribillDetailRow['fine'];        
      	 $total['CrAmount']     += $vepaaribillDetailRow['amount'];    
      }
      $total['totFine']   = ($total['CrFine'] - $total['DrFine']);
	    $total['totAmount'] = ($total['CrAmount'] - $total['DrAmount']);
}
}
/// Selection Of Vepaari Book Net Fine And Net Amount :End
  $billBook = "SELECT billBookId,DATE_FORMAT(billBookDate,'%Y-%m-%d') AS billBookDate,totalFine,totalAmount FROM billbook
                WHERE partyId = ".$partyId."
                  AND DATE_FORMAT(billBookDate,'%Y-%m-%d') <= '".$toDate."'
                ORDER BY billBookId";
  $billBookRes = mysql_query($billBook);
  while($billBook = mysql_fetch_assoc($billBookRes))
  {
    if($billBook['billBookDate'] >= $toDate)
    {
      $veepariBookDr[$drCount]['vepaaribookId']   = "";
      $veepariBookDr[$drCount]['vepaariBookDate'] = $billBook['billBookDate'];
      $veepariBookDr[$drCount]['particulars']     = "Bill No. ".$billBook['billBookId'];
      $veepariBookDr[$drCount]['fine']            = $billBook['totalFine'];
      $veepariBookDr[$drCount]['amount']          = $billBook['totalAmount'];
      $veepariBookDr[$drCount]['deleteLink']      = "";
      $drCount++;
    }
    else
    { //this get opening balance
      $opening['drFine']   += $billBook['totalFine'];
      $opening['drAmount'] += $billBook['totalAmount'];
    }
  }


// This for sort debit array : Start
foreach ($veepariBookDr as $key => $value)
{
  $tempArr[$key] = $value['vepaariBookDate'];
}
array_multisort($tempArr,SORT_ASC,SORT_STRING,$veepariBookDr);
// This for sort debit array : End

// This for sort credit array : Start
foreach ($veepariBookCr as $key => $value)
{
  $tempArr2[$key] = $value['vepaariBookDate'];
}
array_multisort($tempArr2,SORT_ASC,SORT_STRING,$veepariBookCr);
// This for sort credit array : End

//insert Delete OF Opening:Start
if(isset($_POST['delete']))
{
//$fineToStore =   $opening['drFine'] - $opening['crFine'];
//$amtToStore  =   $opening['drAmount'] - $opening['crAmount'];

// following comment done by nidhish if needed then remove the comment

//$fineToStore = $total['totFine'];
//$amtToStore  = $total['totAmount'];
if($fineToStore < 0)
{
  $crDrToStore = 'Dr';
}
else
{
  $crDrToStore = 'Cr';
}
if(isset($_POST['delete']))
{ 
  //Delete Records Before INSERT Opening :Start
  $newOpeningDate       = date("Y-m-d",mktime(0,0,0,substr($toDate,5,2),substr($toDate,8,2)+1,substr($toDate,0,4)));
  $deleteQueryDetailvepaari = " DELETE FROM vepaaribillDetail
                                WHERE vepaariBookMasterId IN 
                                (SELECT vepaariBookMasterId FROM vepaaribillmaster WHERE partyId = ".$partyId." AND vepaariBookDate <= '".$toDate."')";
  $deleteQueryDetailvepaariRes = mysql_query($deleteQueryDetailvepaari);

  // Master Delete :Start
  $deleteQueryVepaari   = "DELETE FROM vepaaribillmaster
                            WHERE partyId = ".$partyId."
                              AND vepaariBookDate <= '".$toDate."'";
  $deleteQueryVepaariRes  = mysql_query($deleteQueryVepaari);
  // Master Delete :Start

  //Delete Records Before INSERT Opening :Start
  //insert Delete OF Opening :End
   $deleteQuerybillBook = "DELETE FROM billbook
                            WHERE partyId = ".$partyId."
                              AND billBookDate <= '".$toDate."'";
   $deleteQuerybillBookRes = mysql_query($deleteQuerybillBook);
}

}
  $smarty = new Smarty;
  require('bottom.php');
  $smarty->assign("l_editParty",$l_editParty);
  $smarty->assign("l_partyName",$l_partyName);
  $smarty->assign("l_partyAddress",$l_partyAddress);
  $smarty->assign("l_partyMobile",$l_partyMobile);
  $smarty->assign("l_partySelect",$l_partySelect);
  $smarty->assign("l_from",$l_from);
  $smarty->assign("l_to",$l_to);
  $smarty->assign("l_date",$l_date);
  $smarty->assign("l_fine",$l_fine);
  $smarty->assign("l_amount",$l_amount);
  $smarty->assign("l_credit",$l_credit);
  $smarty->assign("l_debit",$l_debit);
  $smarty->assign("l_total",$l_total);
  $smarty->assign("l_find",$l_find);
  $smarty->assign("l_delete",$l_delete);
  $smarty->assign("l_edit",$l_edit);
  
  
  $smarty->assign("partyId",$partyId);
  $smarty->assign("partyName",$partyName);
  $smarty->assign("partyAddress",$partyAddress);
  $smarty->assign("partyMobile",$partyMobile);
  $smarty->assign("partyFromDate",$partyFromDate);
  $smarty->assign("veepariBookCr",$veepariBookCr);
  $smarty->assign("veepariBookDr",$veepariBookDr);
  $smarty->assign("total",$total);
  $smarty->assign("toDate",$toDate);
  $smarty->assign("opening",$opening);
  $smarty->assign("billDetailDebitArray",$billDetailDebitArray);
  $smarty->assign("billDetailCreditArray",$billDetailCreditArray);
  $smarty->assign("total",$total);
  $smarty->display('partyEdit.tpl');
?>
